#include "../src/EventFileDriver.h"
#include "../src/KeepTable.h"

#include <stdio.h>

int
main(void)
{
	KeepTable *table = keep_table_new();
	int	   status;

	event_file_driver_set_file(EVENT_FILE_DRIVER(table), "keeptable.efd");
	status = event_file_driver_loader(EVENT_FILE_DRIVER(table));

	if (status != EVENT_FILE_DRIVER_SUCCESS) {
		printf("文件打开失败！code: %d\n将创建新文件 ......\n", status);
		status = event_file_driver_create(EVENT_FILE_DRIVER(table));
	}

	if (status != EVENT_FILE_DRIVER_SUCCESS) {
		printf("无法创建新文件！请检查程序问题。\n");
		event_object_unref(table);
		return status;
	}

	/* 添加测试数据 */
	for (int i = 0; i < 10000; ++i)
		__keep_table_add_log(table, 10000000 + i, 12000 + i);

	/* 打印表 */
	__keep_table_print(table);

	/* 写入文件 */
	status = event_file_driver_dump(EVENT_FILE_DRIVER(table));
	if (status != EVENT_FILE_DRIVER_SUCCESS) {
		printf("无法写入文件！\n");
		event_object_unref(table);
		return status;
	}

	/* 重新打开对象，并打印表 */
	event_object_unref(table);
	table = keep_table_new();

	event_file_driver_set_file(EVENT_FILE_DRIVER(table), "keeptable.efd");
	status = event_file_driver_loader(EVENT_FILE_DRIVER(table));

	if (status != EVENT_FILE_DRIVER_SUCCESS) {
		printf("文件打开失败！code: %d\n", status);
		event_object_unref(table);
		return status;
	}
	__keep_table_print(table);
	event_object_unref(table);
	return 0;
}